<?php
namespace Home\Model;
use Think\Model;
class SysRoleModel extends Model
{
	//规定表名
	protected $trueTableName = 't_role';

	public function get_all()
    {
    	$res = $this->select();
        foreach ($res as $k => $value) {
            $ids = $value['id'];
            // dump($ids);
            $a = D("RolePermission")->get_permiss($ids);
            $permission_name = '';
            if($a){
                foreach ($a as $key => $value) {
                    $permission_name .= $value['permission_name']." ";
                }  
            }
            $res[$k]['permission_name'] = $permission_name;
        }
        return $res;
    }

    public function add_role($role_name,$status)
    {
    	$data['role_name'] = $role_name;
    	$data['status'] = $status;
    	// $data['parent_id'] = $parent_id;
    	$last_id = $this->add($data);
    	return $last_id?$last_id:false;
    }

    public function delete_role($id)
    {
    	$res1 = $this->where(array("id"=>$id))->delete();
        if($res1){
            $res = D("RolePermission")->delete_role($id);
        }
    	return $res?true:false;
    }

    public function select_role($id)
    {
		$res = $this->where(array("id"=>$id))->select();
		return $res?$res:'';
    }

    public function edit_role($id,$role_name,$status)
    {
    	$data['role_name'] = $role_name;
    	$data['status'] = $status;
    	// $data['parent_id'] = $parent_id;
		$res = $this->where(array("id"=>$id))->save($data);
		return $res?true:false;
    }
}